import time
from io import BytesIO
import requests
from moviepy.video.io.VideoFileClip import VideoFileClip,AudioFileClip

# url="https://video.pearvideo.com/mp4/short/20250805/cont-1801712-16058629-hd.mp4"
# headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0"}
# sp = requests.get(url,headers=headers)

# open('爬取梨视频.mp4','wb').write(sp.content)
# print('视频保存完成')


url1 = "https://982546200.ahdohpiechei.com:22443/PyJdMFk5AHl5YgAoGy4Veig7XioCIx4xJXxRLBkVCzkjKg/upgcxcode/10/61/31547196110/31547196110-1-100026.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&nbs=1&os=upos&uipk=5&platform=pc&oi=3682943532&mid=489053619&deadline=1754656925&trid=04967c09a5fd40978f361c9ac5def0cu&gen=playurlv3&og=hw&upsig=ec5ba9b65534832e46b068282dc56e8e&uparams=e,nbs,os,uipk,platform,oi,mid,deadline,trid,gen,og&bvc=vod&nettype=0&bw=745229&f=u_0_0&agrr=1&buvid=8EB4C7B7-877A-27DF-0FD1-13CE36BDFFE692832infoc&build=0&dl=0&orderid=0,3&x-sid=0X11AfcAIGsFp0&x-redirect=0X11A&x-lid=0X11Aa33e5940a2f6&x-st=0X11Ahash"
headers = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
    "referer":"https://www.bilibili.com/video/BV1Mttzz1ErW/?spm_id_from=333.1007.top_right_bar_window_history.content.click&vd_source=86b03c835dda23ba8bd97a82af82ddf8",
    "accept-encoding":"identity",
    "accept-language":"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"
}

proxy = {

}
print("正在获取视频")
resp1 = requests.get(url1,headers=headers)
print('等1秒')
time.sleep(1)
print("正在获取声音")
url2 = "https://xy110x73x238x28xy.mcdn.bilivideo.cn:8082/v1/resource/31547196110-1-30280.m4s?agrr=1&build=0&buvid=8EB4C7B7-877A-27DF-0FD1-13CE36BDFFE692832infoc&bvc=vod&bw=178442&deadline=1754656925&dl=0&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv3&mid=489053619&nbs=1&nettype=0&og=hw&oi=3682943532&orderid=0%2C3&os=08hbv&platform=pc&sign=b595b3&traceid=trzinMKilnSmVe_0_e_N&uipk=5&uparams=e%2Cgen%2Ctrid%2Cmid%2Cdeadline%2Coi%2Cos%2Cog%2Cnbs%2Cuipk%2Cplatform&upsig=c1d8f66e58301f3d5fa6188156cee41a"
resp2 = requests.get(url2,headers=headers)
print('视频请求情况：')
print(resp1.status_code)
print('声音请求情况：')
print(resp2.status_code)

video_buffer = BytesIO(resp1.content)
audio_buffer = BytesIO(resp2.content)
video = VideoFileClip(video_buffer)
audio = AudioFileClip(audio_buffer)
print('正在合并视频和声音')
va = video.with_audio(audio)
print('正在保存视频')
va.write_videofile("最后的视频1.mp4")
print('完成')